const _ = require('lodash')

// function compose (...funcs) {
//   return function (val) {
//     return funcs.reverse().reduce(function (acc, fn) {
//       return fn(acc)
//     }, val)
//   }
// }

const compose = (...funcs) => val => funcs.reverse().reduce((acc, fn) => fn(acc), val)


// 函数组合调试函数
const log = _.curry((tag, val) => {
  console.log(tag, val)
  return val
})

// lodash函数组合
// _.flowRight

// const setArrFirstUpper = compose(_.toUpper, _.first, _.reverse)
const setArrFirstUpper = compose(_.toUpper, log('first'), _.first, log('reverse'), _.reverse)
const r = setArrFirstUpper(['aaa', 'bbb', 'ccc'])
console.log(r)

// 函数组合结合律
const setArrFirstUpper1 = compose(compose(_.toUpper, log('first'), _.first), log('reverse'), _.reverse)
const r1 = setArrFirstUpper1(['aaa', 'bbb', 'ccc'])
console.log(r1)